home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
STRITEMN
/
STRITEMN.C
< prev
Wrap
C/C++ Source or Header
|
1989-02-23
|
3KB
|
87 lines
/*******************************************************
stritemn(s,d,n) is a string routine in assembly
that will extract the Nth item contained
in the source string (seperated by commas)
and move it to the destination string.
File: stritemn.c
History: original by R. Craig Attig, Feb 1989
*******************************************************/
#include "stdio.h"
#include "strings.h"
void
stritemn()
{
asm
{
movem.l 4(a7),a0-a1 ;a0 is source, a1 is dest
move.w 12(a7),d1 ;d1 is n item
subq.w #1,d1 ;adjust d1 (first item is zeroth, really!)
ble.s @stritemn4 ;if n=0 or less, return with item 1
subq.w #1,d1 ;adjust d1 again for comma count.
; No, subq.w #2,d1 doesn't work alone!
stritemn1:
cmp.b #44,(a0) ;look for seperator...#44=","
beq.s @stritemn2 ; ...see how many occurances.
cmp.b #0,(a0) ;look for end of source string...
beq.s @stritemn5 ; ...and return with no results if end.
addq.l #1,a0 ;next char
bra.s @stritemn1
stritemn2:
addq.l #1,a0 ;comma found! point to start of item
dbne d1,@stritemn1 ;keep looking if not nth item
stritemn4:
cmp.b #44,(a0) ;if last char in item, end it
beq.s @stritemn5
cmp.b #0,(a0) ;no more chars in item?
beq.s @stritemn5
move.b (a0)+,(a1)+ ;copy char!
bra.s @stritemn4 ;and repeat until over.
stritemn5:
clr.b (a1) ;terminate dest string
return
}
}
/*******************************************************************/
main()
{
Str255 s,d;
int count;
printf("The string routine Ñstritemn(source,dest,itemNum)Ñ looks at\n");
printf("the source string, and extracts the item requested in itemNum,\n");
printf("placing it into the destination string. Items are seperated by\n");
printf("comma (#44). Here's an example:\n\n");
strcpy(s,"101,102,103,104,105,106,107,108,109");
printf("Source String: %s\n\n",s);
printf(" (click mouse to continue) ");
while (!Button()) ; /* wait for click due to 1/2 window in MF */
printf("\n\n");
for(count=0; count<=11; count++)
{
stritemn(s,d,count);
printf("Item number %d: %s\n",count,d);
}
printf(" (click mouse to continue) ");
while (!Button()) ; /* wait for click due to 1/2 window in MF */
printf("\n\n");
printf("\nNote that if an item 0 is requested, you'll get the first\n");
printf("item. Items requested that exceed the number of items in the\n");
printf("source string come back empty.");
printf("\n\nPlease click mouse to exit...");
while (!Button()) ; /* wait for click */
}